package io.renren.modules.app.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.MapUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONArray;

import io.renren.common.utils.GoodCouponUtil;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.common.utils.StringUtil;
import io.renren.modules.app.entity.GoodCouponEntity;
import io.renren.modules.app.service.GoodCouponService;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.entity.SysCodeEntity;
import io.renren.modules.sys.service.SysCodeService;

/**
 * 
 * 
 * @author chaolemen
 * @email 2698481037@qq.com
 * @date 2017-12-27 19:45:39
 */
@RestController
@RequestMapping("/app/goodcoupon")
public class GoodCouponController  extends AbstractController {
	@Autowired
	private GoodCouponService goodCouponService;
	@Autowired
	private SysCodeService sysCodeService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("app:goodcoupon:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<GoodCouponEntity> goodCouponList = goodCouponService.queryList(query);
		int total = goodCouponService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(goodCouponList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("app:goodcoupon:info")
	public R info(@PathVariable("id") Long id){
		GoodCouponEntity goodCoupon = goodCouponService.queryObject(id);
		
		return R.ok().put("goodCoupon", goodCoupon);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("app:goodcoupon:save")
	public R save(@RequestBody GoodCouponEntity goodCoupon){
		goodCouponService.save(goodCoupon);
		
		return R.ok();
	}
	
	/**
	 * 保存
	 * @throws ParseException 
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/saveList")
	@RequiresPermissions("app:goodcoupon:save")
	public R saveList(@RequestBody String data) throws ParseException{
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
		List<GoodCouponEntity> goodCoupons = new ArrayList<GoodCouponEntity>();
		JSONArray jsonArray = JSONArray.parseArray(data);
		for(int i=0; i<jsonArray.size(); i++){
			Map<String, String> coupon = (Map<String, String>)jsonArray.get(i);
			GoodCouponEntity goodCoupon = new GoodCouponEntity();
			Long category = MapUtils.getLong(coupon, "category");
			goodCoupon.setCategory(category);
			SysCodeEntity sysCodeEntity = sysCodeService.getByCode(category);
			goodCoupon.setTypeName(sysCodeEntity == null ? category+"" : sysCodeEntity.getName());
			goodCoupon.setCommissionRate(MapUtils.getString(coupon, "commission_rate"));
			goodCoupon.setCouponClickUrl(MapUtils.getString(coupon, "coupon_click_url"));
			goodCoupon.setCouponEndTime(dateFormat.parse(coupon.get("coupon_end_time")));
			String couponInfo = MapUtils.getString(coupon, "coupon_info");
			goodCoupon.setCouponInfo(couponInfo);
			if(!StringUtil.isEmpty(couponInfo) ){
				//满19元减10元
				goodCoupon.setCouponPrice(couponInfo.substring(couponInfo.indexOf("减")+1, couponInfo.indexOf("元", couponInfo.indexOf("减"))));
			}
			goodCoupon.setCouponRemainCount(MapUtils.getLong(coupon, "coupon_remain_count"));
			goodCoupon.setCouponStartTime(dateFormat.parse(coupon.get("coupon_start_time")));
			goodCoupon.setCouponTotalCount(MapUtils.getLong(coupon, "coupon_total_count"));
			goodCoupon.setItemDescription(MapUtils.getString(coupon, "item_description"));
			goodCoupon.setItemUrl(MapUtils.getString(coupon, "item_url"));
			goodCoupon.setNick(MapUtils.getString(coupon, "nick"));
			goodCoupon.setNumIid(MapUtils.getLong(coupon, "num_iid"));
			goodCoupon.setPictUrl(MapUtils.getString(coupon, "pict_url"));
			goodCoupon.setSellerId(MapUtils.getInteger(coupon, "seller_id"));
			goodCoupon.setShopTitle(MapUtils.getString(coupon, "shop_title"));
			goodCoupon.setZkFinalPrice(MapUtils.getString(coupon, "zk_final_price"));
			goodCoupon.setVolume(MapUtils.getLong(coupon, "volume"));
			goodCoupon.setUserType(MapUtils.getInteger(coupon, "user_type"));
			goodCoupon.setTitle(MapUtils.getString(coupon, "title"));
			goodCoupon.setSmallImagesth(MapUtils.getString(coupon, "small_imagesth"));
			goodCoupon.setSmallImagest(MapUtils.getString(coupon, "small_imagest"));
			goodCoupon.setSmallImageso(MapUtils.getString(coupon, "small_imageso"));
			goodCoupon.setSmallImagesf(MapUtils.getString(coupon, "small_imagesf"));
			goodCoupons.add(goodCoupon);
		}
		goodCouponService.saveList(goodCoupons);
		return R.ok();
	}
	
	
	/**
	 * 保存
	 * @throws ParseException 
	 */
	@RequestMapping("/importGood")
	@RequiresPermissions("app:goodcoupon:save")
	public R importGood(Integer start,Integer end) throws ParseException{
		List<GoodCouponEntity> goodCoupons = new ArrayList<GoodCouponEntity>();
		int pagestartNO = start%100==0?start/100:start/100+1;
		int pageEndNO  = end%100==0?end/100:end/100+1;
		if(pagestartNO == pageEndNO){
			pageEndNO ++;
		}
		for(int i = pagestartNO; i<pageEndNO; i++){
			goodCoupons.addAll(GoodCouponUtil.getGoodCoupon("", i, 100, "", sysCodeService));
		}
		goodCouponService.saveList(goodCoupons);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("app:goodcoupon:update")
	public R update(@RequestBody GoodCouponEntity goodCoupon){
		goodCouponService.update(goodCoupon);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("app:goodcoupon:delete")
	public R delete(@RequestBody Long[] ids){
		goodCouponService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
